package com.jyy.myserviceapp;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ShellUtils;

public class MyService extends Service {

    IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void setName(String name) throws RemoteException {
            Log.d(LogTag.TAG, "服务端: MyService name: " + name);
        }

        @Override
        public void startApp(String packageName) throws RemoteException {
            Log.d(LogTag.TAG, "服务端: 启动的apk名称: " + packageName);

//            final ShellUtils.CommandResult commandResult = ShellUtils.execCmd("adb shell am start com.jyy.signapp/com.jyy.signapp.MainActivity", true);
//            Log.d(LogTag.TAG, "服务端: 启动app结果: " + commandResult.result
//                    + ",successMsg: " + commandResult.successMsg + ",errorMsg: " + commandResult.errorMsg);

            final Intent intent = new Intent("com.jyy.myserviceapp.MY_CUSTOM_ACTION");
            intent.putExtra("packageName", packageName);
            sendBroadcast(intent);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(LogTag.TAG, "服务端: MyService onStart");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        Log.d(LogTag.TAG, "服务端: MyService onBind");

        return mStub;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(LogTag.TAG, "服务端: MyService onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(LogTag.TAG, "服务端: MyService onDestroy");
    }
}
